import { Component, OnInit } from '@angular/core';
import {NoticeService} from '../notice.service';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { switchMap } from 'rxjs/operators';


@Component({
    selector: 'app-receive-list',
    templateUrl: './receive-list.component.html',
    styleUrls: ['./receive-list.component.scss'],
    providers: [NoticeService]
})

export class ReceiveListComponent implements OnInit {
    private navHeight: string = "0px";
    private notices: any[];
    private contentStyle: any = { 'height': '500px' };
    text: string;
    title: any;
    receiveList: any[];
    totalRecords: number = 0;
    url: string;
    notice: any = {};
    humanId: any;
    urlList: any[] = [];
    isShow: boolean = false;

    constructor(
        private route: ActivatedRoute,
        private noticeService: NoticeService) { }

    ngOnInit() {
        this.humanId = JSON.parse(localStorage.getItem('currentHumanInfo')).humanId;
        let noticeContentHeight = $("qk-notice").parent().height();
        this.navHeight = (noticeContentHeight - 88) + "px";
        this.contentStyle.height = (noticeContentHeight - 60) + "px";
        this.url = location.origin+'/media/api/upload';
        let afterUrl = decodeURI(window.location.search).substring(1);
        let strings = afterUrl.split('=');
        let params = strings[1];
        this.searchByTitle();
        setTimeout(() => {
            this.readNotice({'noticeId': params});
        }, 1000);
    }

    /** 加载通知数据 */
    loadData(params) {
        this.noticeService.loadReceiveList(params).subscribe(data => {
                this.receiveList = data ? data.list : [];
                this.totalRecords = data ? data.total : 0;
                if (this.receiveList.length < 1) {
                    this.isShow = true;
                } else {
                    this.isShow = false;
                }
        });
    }

    searchByTitle() {
            this.text = null;
            let params = {};
            params['title'] = this.title ? this.title : '';
            params['pageSize'] = 15;
            params['pageNum'] = 1;
            params['humanId'] = this.humanId;
            this.loadData(params);
    }

    /** 点击加载下一页数据 */
    loadNoticeList(event) {
        let params = {};
        params['pageSize'] = 15;
        params['humanId'] = this.humanId;
        params['title'] = this.title ? this.title : '';
        params['pageNum'] = event ? event.first / event.rows + 1 : 1;
        this.loadData(params);
    }

    readNotice(event) {
        this.receiveList.forEach(e => {
            if (e.noticeId == event.noticeId) {
                event = e;
                e['style'] = {'background-color': '#007ad9', 'color': '#FFFFFF', 'cursor': 'pointer'};
            } else {
                e['style'] = {'background-color': '#FFFFFF', 'color': '#333333', 'cursor': 'pointer'};
            }
        });
        if (event.noticeId > 0) {
            this.urlList = [];
            let idList = [];
            if (event.attachmentIds != '' && event.attachmentIds != null) {
                idList = event.attachmentIds.split('/');
                this.noticeService.getUrlById(idList).subscribe( data => this.urlList = data);
            }
            this.text = '<h1 class="ql-align-center">' + event.title + '</h1>' + '<p style="text-align: right;">' + event.createTime + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + event.publishUnit + '</p>' + event.content;
            if (event.isRead === 0) {
                this.noticeService.haveRead(event.noticeId).subscribe( data => {
                        event.isRead = this.humanId;
                });
            }
        }
    }

    enterOnkeydown($event) {
        if ($event.keyCode == 13) {
            this.searchByTitle();
        }
    }

    /** 附件下载 */
    download(event) {
        this.noticeService.downland(event.mediaPath, event.mediaName).subscribe(data => console.log('成功！'));
    }
}
